/* The file is saved in UTF-8 codepage.
 * Check: «Stereotype», Section mark-§, Copyright-©, Alpha-α, Beta-β, Smile-☺
 */
package cz.pecinovsky.english.lootp.utility;



////////////////////////////////////////////////////////////////////////////////
//%%+  +++++ End of ignored starting test - place for imports +++++

//%X+ xxxxx Start of the ovejumped text with declaration of the envelope xxxxx
/*******************************************************************************
 *<pre>
 * Předchozí:   No - this is a newly defined type
 *              Ttt in the project Ppp
 * Následující: No
 *
 * Project  Ppp
 *   + Adeed
 *   - Removed
 *   ~ Changed
 *</pre>
 *
 * @author Rudolf PECINOVSKÝ
 * @version 6.03.2017 — 2010-11-08
 */
////////////////////////////////////////////////////////////////////////////////
//%X- ----- End of the ovejumped text with declaration of the envelope -----


/*******************************************************************************
 * Instances of class {@code Position} are trasport objects (crates)
 * containing coordinates.
 * Their attributes are therefore defined as public constants.
 *
 * @author Rudolf PECINOVSKÝ
 * @version 6.03.2017 — 2010-11-08
 */
public class Position
{
//== CONSTANT CLASS ATTRIBUTES =================================================
//== VARIABLE CLASS ATTRIBUTES =================================================
//== STATIC CONSTRUCTOR (CLASS INITIALIZER, STATIC INICITALIZING BLOCK) ========
//== CONSTANT INSTANCE ATTRIBUTES ==============================================

    /** Horizontal coordinate. */
    public final int x;

    /** Vertical coordinate. */
    public final int y;



//== VARIABLE INSTANCE ATTRIBUTES===============================================
//== CLASS GETTERS AND SETTERS =================================================
//== OTHER NON-PRIVATE CLASS METHODS ===========================================

//##############################################################################
//== CONSTUCTORS AND FACTORY METHODS============================================

    /***************************************************************************
     * Creates a crate containing given coordinates.
     *
     * @param x Horizontal coordinate
     * @param y Vertical coordinate
     */
    public Position(int x, int y)
    {
        this.x = x;
        this.y = y;
    }



//== ABSTRACT METHODS ==========================================================
//== INSTANCE GETTERS AND SETTERS ==============================================

    /***************************************************************************
     * Returns the horizontal coordinate.
     *
     * @return  The horizontal coordinate
     */
    public int getX()
    {
        return x;
    }


    /***************************************************************************
     * Returns the vertical coordinate.
     *
     * @return  The vertical coordinate
     */
    public int getY()
    {
        return y;
    }



//== OTHER NON-PRIVATE INSTANCE METHODS ========================================
//%A+ >127

    /***************************************************************************
     * Indicates whether the object referred by the parameter
     * represets the same position.
     *
     * @param object The object with which to compare
     * @return {@code true} if this object points to the same position
     *         as the argument object; {@code false} otherwise.
     */
    @Override
    public boolean equals(Object object)
    {
//%A-
//%I+ >127 -135
//        return (object instanceof Position)      &&
//               (((Position)object).x == this.x)  &&
//               (((Position)object).y == this.y);
//%I-
//%A+ >135
        if (! (object instanceof Position)) {
            return false;               //==========>
        }
        Position other = (Position)object;
        return (other.x == this.x)  &&  (other.y == this.y);
//%A-
//%A+ >127
    }

//%A-
//%X+ <999

    /***************************************************************************
     * Returns the hash code of the instance.
     *
     * @return  Hash code of the instance
     */
    @Override
    public int hashCode()
    {
        return 1234567 + 37*(37*x + y);
    }

//%X-
//%X+ <126

    /***************************************************************************
     * Returns a string representation of the instance.
     * It is used mostly for debugging purposes.
     *
     * @return String representation of the instance
     */
    @Override
    public String toString()
    {
        return "Position[x=" + x + ", y=" + y + "]";
    }

//%X-
//%X+ <999

    /***************************************************************************
     * Returns a new position pointing to positiona after movement by
     * the given amount of points horizontaly and verticaly.
     *
     * @param  dx   Horizontal distance of the move (>0=right, <0=left)
     * @param  dy   Horizontal distance of the move (>0=down,  <0=up)
     * @return Position after the given movement
     */
    public Position moveBy(int dx, int dy)
    {
        return new Position(x+dx, y+dy);
    }


    /***************************************************************************
     * Returns a new position pointing to positiona after movement by
     * the amount of points given by the coordinates of the argument.
     *
     * @param  distance Relative distance of the requested destination position
     *                  to the actual one
     * @return Position after the given movement
     */
    public Position moveBy(Position distance)
    {
        return new Position(x+distance.x, y+distance.y);
    }


    /***************************************************************************
     * Returns the distance to the position given in argument.
     *
     * @param  pozice  Position we are measureing distance to it
     * @return The measured distance
     */
    public double distanceTo(Position pozice)
    {
        return Math.hypot(x-pozice.x, y-pozice.y);
    }

//%X-
//%X+ <126


//%X-
//== PRIVATE AND AUXILIARY CLASS METHODS =======================================
//== PRIVATE AND AUXILIARY INSTANCE METHODS ====================================
//== EMBEDDED AND INNER CLASSES ================================================
//== TESTING CLASSES AND METHODS ===============================================
//
//    /***************************************************************************
//     * Test method.
//     */
//    public static void test()
//    {
//        Position inst = new Position();
//    }
//    /** @param args Command line arguments - they are not used. */
//    public static void main(String[] args)  {  test();  }
}
